package com.codetree.strategy.controller;

import com.codetree.strategy.functionalInterface.PaymentFunctionService;
import com.codetree.strategy.functionalInterface.domain.PaymentResult;
import com.codetree.strategy.simple.if_example.IfPaymentService;
import com.codetree.strategy.simple.strategy_example.PaymentService;
import com.codetree.strategy.simple.strategy_example.PaymentStrategyFactory;
import com.codetree.strategy.simple.switch_example.SwitchPaymentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/pay")
public class PaymentController {

    @Resource
    private final PaymentStrategyFactory paymentStrategyFactory;
    private final IfPaymentService ifService;
    private final SwitchPaymentService switchService;
    private final PaymentFunctionService paymentFunctionService;

    public PaymentController(PaymentStrategyFactory paymentStrategyFactory,
                             IfPaymentService ifService,
                             SwitchPaymentService switchService, PaymentFunctionService paymentFunctionService) {
        this.paymentStrategyFactory = paymentStrategyFactory;
        this.ifService = ifService;
        this.switchService = switchService;
        this.paymentFunctionService = paymentFunctionService;
    }


    @PostMapping("/if")
    public String ifPay(@RequestParam String type,
                        @RequestParam BigDecimal amount) {
        return ifService.pay(type, amount);
    }

    @PostMapping("/switch")
    public String switchPay(@RequestParam String type,
                            @RequestParam BigDecimal amount) {
        return switchService.pay(type, amount);
    }


    @PostMapping("/strategy")
    public String strategyPay(@RequestParam String type,
                              @RequestParam BigDecimal amount) {
        PaymentService strategy = paymentStrategyFactory.getStrategy(type);
        if (strategy == null) {
            throw new IllegalArgumentException("无效支付方式");
        }
        return strategy.pay(type, amount);
    }

    @PostMapping("/function")
    public PaymentResult functionPay(@RequestParam String type,
                                     @RequestParam BigDecimal amount) {
        return paymentFunctionService.executePayment(type, amount);
    }
}
